//
//  AccountBalanceVC.m
//  yunbaolive
//
//  Created by ybRRR on 2020/2/6.
//  Copyright © 2020 cat. All rights reserved.
//

#import "AccountBalanceVC.h"
#import "BillCell.h"
#import "BuyerGetMoneyVC.h"
@interface AccountBalanceVC ()<UITableViewDelegate, UITableViewDataSource>
{
    UIView *bottomView;
    UILabel *bottomPrice;  //底部账户余额
    UILabel *balanceLb; //账户余额
    int pageIndex;
    NSDictionary *user_balance;
}
@property(nonatomic, strong)UITableView *balanceTable;
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)NSMutableArray *infoArr;
@end

@implementation AccountBalanceVC

-(void)requestBalanceData{
    NSDictionary *signdic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken], @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]]};
    NSString *sign = [PublicObj sortString:signdic];
    NSDictionary *dic = @{
                          @"uid":[Config getOwnID],
                          @"token":[Config getOwnToken],
                          @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]],
                          @"sign":sign,
                          @"p":@(pageIndex)
                          };
    
    [WYNetworking postWithUrl:@"Buyer.getRefundList" Dic:dic Suc:^(int code, id info, NSString *msg) {
     if (code ==0) {
         NSDictionary *infos = [info firstObject];
         NSArray *listArr = [infos valueForKey:@"list"];
         user_balance =[infos valueForKey:@"user_balance"];
         [self reloadCurrentBalance:user_balance];
         if (pageIndex == 1) {
             [self.infoArr removeAllObjects];

             if (listArr.count < 1) {
                 [PublicView showImgNoData:self.balanceTable name:@"shop_无数据" text:YZMsg(@"无相关退款记录")];
                 [self.balanceTable reloadData];
                 return ;
             }else{
                 [PublicView hiddenImgNoData:self.balanceTable];
             }
         }
         [self.infoArr addObjectsFromArray:listArr];
         [self.balanceTable reloadData];
     }else{
         [MBProgressHUD showError:msg];
     }

        } Fail:^(id fail) {
            
     }];
}
-(void)reloadCurrentBalance:(NSDictionary *)dic{
    bottomPrice.text =[NSString stringWithFormat:@"¥%@",minstr([dic valueForKey:@"balance"])];
    balanceLb.text = [NSString stringWithFormat:@"¥%@",minstr([dic valueForKey:@"balance"])];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self requestBalanceData];

}
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0,*)) {
        return UIStatusBarStyleDarkContent;
    }
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleL.text =YZMsg(@"账户余额");
    self.view.backgroundColor = RGB(245, 245, 245);
    self.subNavi.backgroundColor = UIColor.whiteColor;
    self.titleL.textColor = UIColor.blackColor;
    [self.leftBtn setImage:[UIImage imageNamed:@"pub_back_black"] forState:0];
    self.naviLine.hidden = NO;
    self.naviLine.backgroundColor = RGB(245, 245, 245);

    pageIndex = 1;
    self.infoArr = [NSMutableArray array];
    [self.view addSubview:self.balanceTable];
    [self creatBottomView];
}
#pragma mark-------底部view-------
-(void)creatBottomView{
    bottomView = [[UIView alloc]init];
    bottomView.frame = CGRectMake(0, _window_height-60, _window_width, 60);
    bottomView.backgroundColor =UIColor.whiteColor;
    [self.view addSubview:bottomView];
    
    UILabel *titleLb = [[UILabel alloc]init];
    titleLb.frame = CGRectMake(12, 10, 100, 16);
    titleLb.font = [UIFont systemFontOfSize:13];
    titleLb.textColor = [UIColor blackColor];
    titleLb.text = YZMsg(@"账户余额");
    [bottomView addSubview:titleLb];
    
    bottomPrice =[[UILabel alloc]init];
    bottomPrice.frame = CGRectMake(12, titleLb.bottom+5, 150, 20);
    bottomPrice.font = [UIFont boldSystemFontOfSize:16];
    bottomPrice.textColor = [UIColor blackColor];
    [bottomView addSubview:bottomPrice];
    
    //收益
    UIButton *profitBtn = [UIButton buttonWithType:0];
    profitBtn.frame = CGRectMake(_window_width-85, bottomView.height/2-13, 75, 26);
    [profitBtn setBackgroundColor:Pink_Cor];
    [profitBtn setTitle:YZMsg(@"提取余额") forState:UIControlStateNormal];
    profitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    profitBtn.layer.cornerRadius = 13;
    profitBtn.layer.masksToBounds = YES;
    [profitBtn setTitleColor:[UIColor whiteColor] forState:0];
    [profitBtn addTarget:self action:@selector(profitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:profitBtn];
    
    //提现
    UIButton *cashBtn = [UIButton buttonWithType:0];
    cashBtn.frame = CGRectMake(profitBtn.left-85, bottomView.height/2-13, 75, 26);
    cashBtn.layer.borderColor = Pink_Cor.CGColor;
    cashBtn.layer.borderWidth = 1;
    cashBtn.layer.cornerRadius = 13;
    cashBtn.layer.masksToBounds = YES;
    [cashBtn setTitle:YZMsg(@"提现记录") forState:UIControlStateNormal];
    cashBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [cashBtn setTitleColor:Pink_Cor forState:0];
    [cashBtn addTarget:self action:@selector(cashBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:cashBtn];

}
#pragma mark------提取记录--------
-(void)cashBtnClick{
    //用户体现记录
    NSString *url =[NSString stringWithFormat:@"%@/appapi/shopcash/index",h5url];
    PubH5 *h5VC = [[PubH5 alloc]init];
    h5VC.url =  [self addurl:url];
    [[YBBaseAppDelegate sharedAppDelegate]pushViewController:h5VC animated:YES];

}
//所有h5需要拼接uid和token
-(NSString *)addurl:(NSString *)url{
    return [url stringByAppendingFormat:@"&uid=%@&token=%@",[Config getOwnID],[Config getOwnToken]];
}
#pragma mark-------提取余额----------
-(void)profitBtnClick{
    BuyerGetMoneyVC *getVC = [[BuyerGetMoneyVC alloc]init];
    getVC.moneyDic = user_balance;
    [[YBBaseAppDelegate sharedAppDelegate]pushViewController:getVC animated:YES];

}
-(UITableView *)balanceTable{
    if (!_balanceTable) {
        _balanceTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight-60) style:UITableViewStylePlain];
        _balanceTable.delegate = self;
        _balanceTable.dataSource = self;
        _balanceTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _balanceTable.backgroundColor = [UIColor clearColor];
        _balanceTable.tableFooterView = [UIView new];
    }
    return _balanceTable;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 130;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BillCell *cell = [BillCell cellWithTab:tableView andIndexPath:indexPath];
    cell.infoDic =self.infoArr[indexPath.row];
    return  cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    return self.headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]init];
        _headerView.frame = CGRectMake(0, 0, _window_width, 130);
        _headerView.backgroundColor = UIColor.whiteColor;
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _headerView.height-40)];
        backView.backgroundColor = Pink_Cor;
        [_headerView addSubview:backView];
        
        UILabel *title1 = [[UILabel alloc]init];
        title1.frame = CGRectMake(12, 15, _window_width/2, 20);
        title1.font = [UIFont systemFontOfSize:14];
        title1.textColor = RGBA(255,255,255,0.5);
        title1.text = YZMsg(@"账户余额 (元)");
        [backView addSubview:title1];
        

        balanceLb = [[UILabel alloc]init];
        balanceLb.frame = CGRectMake(12, title1.bottom + 10, _window_width/2-10, 20);
        balanceLb.font = [UIFont systemFontOfSize:15];
        balanceLb.textColor = [UIColor whiteColor];
        [backView addSubview:balanceLb];

        
        UILabel *bottomLb = [[UILabel alloc]init];
        bottomLb.frame = CGRectMake(12, _headerView.height-30, _window_width-20, 20);
        bottomLb.text = YZMsg(@"退款记录");
        bottomLb.font = [UIFont boldSystemFontOfSize:14];
        bottomLb.textColor = [UIColor grayColor];
        [_headerView addSubview:bottomLb];
    }
    return _headerView;
}


@end
